/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import com.lineage.configure.ConfigureSQL;
import com.lineage.configure.ConfigureServer;
import com.lineage.server.GameServer;

/**
 * Class <code>StartServer</code> 启动服务器.
 * 
 * @author jrwz
 * @version 2012-5-23下午04:26:05
 * @see com.lineage.server
 * @since JDK1.7
 */
public final class StartServer {

    /** 提示信息. */
    private static final Logger LOG = Logger.getLogger(StartServer.class
            .getName());

    /** 纪录档的路径. */
    private static final String LOG_PROP = "./config/log.properties";

    /**
     * 启动服务器.
     * 
     * @param args
     *            参数
     * @throws Exception
     *             构造详细消息为 null 的新异常。
     *             原因尚未被初始化，
     *             可在以后通过调用 Throwable.initCause(java.lang.Throwable) 对其进行初始化。
     */
    public static void main(final String[] args) throws Exception {

        final File logFolder = new File("log");
        logFolder.mkdir();

        try {
            final InputStream is = new BufferedInputStream(new FileInputStream(
                    LOG_PROP));
            LogManager.getLogManager().readConfiguration(is);
            is.close();
        } catch (final IOException e) {
            LOG.log(Level.SEVERE, "无法加载 " + LOG_PROP + " 文件.", e);
            System.exit(0); // 程序结束
        }

        try {
            ConfigureServer.load(); // 加载服务器配置
            ConfigureSQL.load(); // 加载数据库配置
        } catch (final Exception e) {
            LOG.log(Level.SEVERE, e.getLocalizedMessage(), e);
            System.exit(0); // 程序结束
        }

        L1DatabaseFactory.setDatabaseSettings(ConfigureSQL.DB_DRIVER,
                ConfigureSQL.DB_URL, ConfigureSQL.DB_LOGIN,
                ConfigureSQL.DB_PASSWORD);
        L1DatabaseFactory.getInstance();
        GameServer.getInstance().initialize();
    }

    /** 启动服务器. */
    private StartServer() {
    }
}
